// 操行积分类型定义
export interface ComprehensiveScore {
  id: number;
  studentId: string;
  studentName: string;
  semester: string;
  bonusScore: number; // 加分项
  bonusReason: string; // 加分原因
  penaltyScore: number; // 减分项
  penaltyReason: string; // 减分原因
  totalScore: number; // 总积分
  evaluation: string; // 评价等级
  updateDate: string; // 更新日期
}

// 模拟数据
 export let mockScores: ComprehensiveScore[] =[  
  {
    id: 1,
    studentId: '2023001',
    studentName: '张三',
    semester: '2023-2024第一学期',
    bonusScore: 15,
    bonusReason: '学习成绩优异，积极参与课外活动',
    penaltyScore: 0,
    penaltyReason: '',
    totalScore: 95,
    evaluation: '优秀',
    updateDate: '2024-01-20'
  },
  {
    id: 2,
    studentId: '2023002',
    studentName: '李四',
    semester: '2023-2024第一学期',
    bonusScore: 8,
    bonusReason: '学习态度认真，团队合作能力强',
    penaltyScore: 0,
    penaltyReason: '',
    totalScore: 88,
    evaluation: '良好',
    updateDate: '2024-01-20'
  },
  {
    id: 3,
    studentId: '2023003',
    studentName: '王五',
    semester: '2023-2024第一学期',
    bonusScore: 5,
    bonusReason: '学习成绩优秀',
    penaltyScore: 0,
    penaltyReason: '',
    totalScore: 85,
    evaluation: '良好',
    updateDate: '2024-01-20'
  },
  {
    id: 4,
    studentId: '2023004',
    studentName: '赵六',
    semester: '2023-2024第一学期',
    bonusScore: 2,
    bonusReason: '积极参与班级活动',
    penaltyScore: 3,
    penaltyReason: '偶尔迟到',
    totalScore: 79,
    evaluation: '中等',
    updateDate: '2024-01-20'
  },
  {
    id: 5,
    studentId: '2023005',
    studentName: '孙七',
    semester: '2023-2024第一学期',
    bonusScore: 0,
    bonusReason: '',
    penaltyScore: 8,
    penaltyReason: '多次违反课堂纪律',
    totalScore: 72,
    evaluation: '中等',
    updateDate: '2024-01-20'
  },
  {
    id: 6,
    studentId: '2023006',
    studentName: '周八',
    semester: '2023-2024第一学期',
    bonusScore: 12,
    bonusReason: '各方面表现均衡，有潜力进一步提升',
    penaltyScore: 0,
    penaltyReason: '',
    totalScore: 92,
    evaluation: '优秀',
    updateDate: '2024-01-20'
  },
  {
    id: 7,
    studentId: '2023007',
    studentName: '吴九',
    semester: '2023-2024第二学期',
    bonusScore: 18,
    bonusReason: '全面发展的优秀学生代表',
    penaltyScore: 0,
    penaltyReason: '',
    totalScore: 98,
    evaluation: '优秀',
    updateDate: '2024-06-20'
  },
  {
    id: 8,
    studentId: '2023008',
    studentName: '郑十',
    semester: '2023-2024第二学期',
    bonusScore: 0,
    bonusReason: '',
    penaltyScore: 5,
    penaltyReason: '需在学业上投入更多精力',
    totalScore: 75,
    evaluation: '中等',
    updateDate: '2024-06-20'
  }
];

// 获取所有操行积分记录
export const getScores = async (): Promise<ComprehensiveScore[]> => {
  // 模拟API延迟
  await new Promise(resolve => setTimeout(resolve, 300));
  return [...mockScores];
};

// 计算总积分和评价等级
export const calculateTotalAndEvaluation = (scores: any): { totalScore: number; evaluation: string } => {
  const baseScore = 80; // 默认基础分为80分
  const totalScore = baseScore + (scores.bonusScore || 0) - (scores.penaltyScore || 0);
  let evaluation = '不及格';
  
  if (totalScore >= 90) evaluation = '优秀';
  else if (totalScore >= 80) evaluation = '良好';
  else if (totalScore >= 70) evaluation = '中等';
  else if (totalScore >= 60) evaluation = '及格';
  
  return { totalScore, evaluation };
};

// 添加操行积分记录
export const addScore = async (scoreData: Omit<ComprehensiveScore, 'id' | 'totalScore' | 'evaluation'>): Promise<ComprehensiveScore> => {
  // 模拟API延迟
  await new Promise(resolve => setTimeout(resolve, 300));
  
  const { totalScore, evaluation } = calculateTotalAndEvaluation(scoreData);
  const newScore: ComprehensiveScore = {
    id: mockScores.length > 0 ? Math.max(...mockScores.map(s => s.id)) + 1 : 1,
    ...scoreData,
    totalScore,
    evaluation
  };
  
  mockScores.push(newScore);
  return newScore;
};

// 更新操行积分记录
export const updateScore = async (id: number, scoreData: Omit<ComprehensiveScore, 'id' | 'totalScore' | 'evaluation'>): Promise<ComprehensiveScore> => {
  // 模拟API延迟
  await new Promise(resolve => setTimeout(resolve, 300));
  
  const index = mockScores.findIndex(score => score.id === id);
  if (index === -1) {
    throw new Error('未找到该操行积分记录');
  }
  
  const { totalScore, evaluation } = calculateTotalAndEvaluation(scoreData);
  const updatedScore: ComprehensiveScore = {
    id,
    ...scoreData,
    totalScore,
    evaluation
  };
  
  mockScores[index] = updatedScore;
  return updatedScore;
};

// 删除操行积分记录
export const deleteScore = async (id: number): Promise<boolean> => {
  // 模拟API延迟
  await new Promise(resolve => setTimeout(resolve, 300));
  
  const index = mockScores.findIndex(score => score.id === id);
  if (index === -1) {
    throw new Error('未找到该操行积分记录');
  }
  
  mockScores.splice(index, 1);
  return true;
};